/**
 * This class houses all logic used by the marketplace, such as price
 * calculation.
 * 
 * @author Stanley Ayzenberg, Michelle Partogi, Adam Perez, Araya Zaesim
 * @version 1.0
 */

import java.util.Random;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Araya Zaesim, Stanley Ayzenberg, Adam Perez, Michelle Partogi
 * @version 1.0
 */
public class Marketplace extends JPanel {

	/**
	 * Field btnBuyAll.
	 */
	private final JButton[] btnBuyAll;

	/**
	 * Field btnSellAll.
	 */
	private final JButton[] btnSellAll;

	/**
	 * Field btnSellOne.
	 */
	private final JButton[] btnSellOne;

	/**
	 * Field btnBuyOne.
	 */
	private final JButton[] btnBuyOne;

	/**
	 * Field lblSellPrice.
	 */
	private final JLabel[] lblSellPrice;

	/**
	 * Field lblBuyPrice.
	 */
	private final JLabel[] lblBuyPrice;

	/**
	 * Field player.
	 */
	public Player player = null;

	/**
	 * Field currentSystem.
	 */
	private SolarSystem currentSystem;

	/**
	 * Field goodNames.
	 */
	private final JLabel[] goodNames;

	/**
	 * Field buyPrices.
	 */
	private int[] buyPrices;

	/**
	 * Field sellPrices.
	 */
	private int[] sellPrices;

	/**
	 * Field gui.
	 */
	public SpaceTraderGui gui = null;

	/**
	 * Field event.
	 */
	protected String event = null;
	
	/**
	 * Int num goods is how many goods there are.
	 */
	private int numGoods;
	
	/**
	 * Maximum value to randomize.
	 */
	private static final int MAX_RAND = 100;
	
	/**
	 * Constant random number to check.
	 */
	private static final int RAND_CHECK = 10;
	
	/**
	 * Constant for number of events that occur.
	 */
	private static final int EVENT_TYPES = 7;
	
	/**
	 * Constant for dimensions x of grid.
	 */
	private static final int GRIDDIMONE = 11;
	
	/**
	 * Constant for dimensions y of grid.
	 */
	private static final int GRIDDIMTWO = 7;
	
	/**
	 * X dimension of panel
	 */
	private static final int DIM_X = 700;
	
	/**
	 * Y dimension of panel
	 */
	private static final int DIM_Y = 600;
	
	/**
	 * Number of possible goods.
	 */
	private static final int NUM_GOODS = 10;

	/**
	 * Method eventCreate.
	
	 * @return String */
	public static String eventCreate() {
		String event;
		final Random rand = new Random();
		if (rand.nextInt(MAX_RAND) < RAND_CHECK) {
			final int index = rand.nextInt(EVENT_TYPES);
			final String[] array = { "Lack of Workers", "Boredom", "Plague", "War",
					"Cropfail", "Cold", "Drought" };
			event = array[index];
		} else {
			event = "None";
		}
		return event;
	}

	/**
	 * Constructor for Marketplace.
	 * @param player Player
	 * @param system SolarSystem
	 * @param gui SpaceTraderGui
	 */

	public Marketplace(Player player, SolarSystem system, SpaceTraderGui gui) {
		this(player, system, gui, eventCreate());
	}

	/**
	 * Constructor for Marketplace.
	 * @param player Player
	 * @param system SolarSystem
	 * @param gui SpaceTraderGui
	
	 * @param event String
	 */
	public Marketplace(Player player, SolarSystem system, 
			SpaceTraderGui gui, String event) {
		this.gui = gui;
		this.player = player;
		this.event = event;
		setLayout(new GridLayout(GRIDDIMONE, GRIDDIMTWO));
		setPreferredSize(new Dimension(DIM_X, DIM_Y));
		
		this.currentSystem = system; //++
		final MarketPlaceLogic mpl = new MarketPlaceLogic (system, event); //++
		setPreferredSize(new Dimension(DIM_X, DIM_Y));

		this.buyPrices = mpl.buyPrices();
		this.sellPrices = mpl.sellPrices();
		
		final int[] tempGoods2 = currentSystem.getGoods();
		final String[] goods = { "Water", "Furs", "Food", "Ore", "Games", "Firearms",
				"Medicine", "Machines", "Narcotics", "Robots" };
		this.numGoods = goods.length;
		btnBuyAll = new JButton[numGoods];
		lblSellPrice = new JLabel[numGoods];
		lblBuyPrice = new JLabel[numGoods];
		goodNames = new JLabel[numGoods];
		btnSellAll = new JButton[numGoods];
		btnBuyOne = new JButton[numGoods];
		btnSellOne = new JButton[numGoods];
		for (int i = 0; i < numGoods; i++){
			//creates all the Goods Selling Price Labels
			lblSellPrice[i] = new JLabel("$" + sellPrices[i]);
			
			//creates all the Goods Selling Price Labels
			lblBuyPrice[i] = new JLabel("$" + buyPrices[i]);
			goodNames[i] = new JLabel(goods[i]);

			// creates all the "Buy All" buttons
			System.out.println("hi");
			btnBuyAll[i] = new JButton("Max");
			btnBuyAll[i].addActionListener(new BuyAll(i));

			// creates all the "Sell All" buttons
			btnSellAll[i] = new JButton("All");
			btnSellAll[i].addActionListener(new SellAll(i));

			// creates all the "Sell One" buttons
			btnSellOne[i] = new JButton("0"); // how much you have in cargo
			btnSellOne[i].addActionListener(new SellOne(i));

			// creates all the "Buy One" buttons
			btnBuyOne[i] = new JButton("" + tempGoods2[i]); // how much solar
															// system has
			btnBuyOne[i].addActionListener(new BuyOne(i));
		}

		final String[] title = { "Goods", "Sell Price", "Sell", "Sell All",
				"Buy Price", "Buy", "Buy All" };
		final JLabel[] lblTitle = new JLabel[title.length];
		for (int i = 0; i < title.length; i++) {
			lblTitle[i] = new JLabel(title[i]);
			add(lblTitle[i]);
			lblTitle[i].setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		}
		for (int i = 0; i < numGoods; i++){
			add(goodNames[i]);

			add(lblSellPrice[i]);
			lblSellPrice[i].setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
			add(btnSellOne[i]);
			add(btnSellAll[i]);

			add(lblBuyPrice[i]);
			lblBuyPrice[i].setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
			add(btnBuyOne[i]);
			add(btnBuyAll[i]);

		}

	}

	/**
	 * Method newMarketPlace.
	 * @param system SolarSystem
	 */
	public void newMarketPlace(SolarSystem system) {
		this.currentSystem = system;
		final MarketPlaceLogic mpl = new MarketPlaceLogic(system, eventCreate());
		this.buyPrices = mpl.buyPrices();
		this.sellPrices = mpl.sellPrices();

		final int[] tempGoods = player.getShip().getGoods();
		final int[] tempGoods2 = currentSystem.getGoods();

		for (int i = 0; i < NUM_GOODS; i++) {
			// creates all the Goods Selling Price Labels
			lblSellPrice[i].setText("   $" + sellPrices[i]);

			// creates all the Goods Selling Price Labels
			lblBuyPrice[i].setText("   $" + buyPrices[i]);

			// creates all the "Sell One" buttons
			btnSellOne[i].setText("" + tempGoods[i]); // how much you have in
														// cargo

			// creates all the "Buy One" buttons
			btnBuyOne[i].setText("" + tempGoods2[i]); // how much solar system
														// has
		}
	}

	/**
	 * Method updateMarketPlace.
	 * @param player Player
	 */
	public void updateMarketPlace(Player player) {
		gui.setCredits(player.getCredits());
		// make this call a gui function
		final int[] currentGoods = player.getShip().getGoods();

		for (int i = 0; i < NUM_GOODS; i++) {
			btnSellOne[i].setText("" + currentGoods[i]);
		}

		if (currentSystem != null) {
			final int[] systemGoods = currentSystem.getGoods();
			for (int i = 0; i < NUM_GOODS; i++) {
				btnBuyOne[i].setText("" + systemGoods[i]);
			}
		}

	}

	/**
	 * 
	 * @author Araya Zaesim, Stanley Ayzenberg, Adam Perez, Michelle Partogi
	 */
	private class SellOne implements ActionListener {
		/**
		 * toString method.
		
		 * @return String */
		public String toString() {
			return "";
		}
		
		/**
		 * Field index.
		 */
		private final int index;

		/**
		 * Constructor for SellOne.
		 * @param index int
		 */
		private SellOne(int index) {
			this.index = index;

		}

		/**
		 * Method actionPerformed.
		 * @param e ActionEvent
		
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
		public void actionPerformed(ActionEvent e) {
			final int[] tempGoods = player.getShip().getGoods();
			final int[] tempGoods2 = currentSystem.getGoods();
			if (tempGoods[index] > 0 && tempGoods2[index] > 0
					&& Goods.FOOD.getMTLU() <= currentSystem.getTechLevel()) {
				tempGoods[index] -= 1;
				tempGoods2[index] += 1;
				player.getShip().setGoods(tempGoods);
				player.getShip().setCargo(player.getShip().getCargo() - 1);
				currentSystem.setGoods(tempGoods2);
				player.setCredits(player.getCredits() + sellPrices[index]);
			}
			updateMarketPlace(player);
		}
	}

	/**
	 * @author Araya Zaesim, Stanley Ayzenberg, Adam Perez, Michelle Partogi
	 */
	private class BuyAll implements ActionListener {
		/**
		 * toString method.
		
		 * @return String */
		public String toString() {
			return "";
		}
		
		/**
		 * Field index.
		 */
		private final int index;

		/**
		 * Constructor for BuyAll.
		 * @param index int
		 */
		private BuyAll(int index) {
			this.index = index;
		}

		/**
		 * Method actionPerformed.
		 * @param e ActionEvent
		
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
		public void actionPerformed(ActionEvent e) {
			final int[] tempGoods = player.getShip().getGoods();
			final int[] tempGoods2 = currentSystem.getGoods();
			final boolean isFalse = false;
			while (player.getShip().isFull() == isFalse
					&& player.getCredits() >= buyPrices[index]
					&& tempGoods2[index] > 0) { // ADD PRICE for credits
				tempGoods[index] += 1;
				tempGoods2[index] -= 1;
				player.getShip().setGoods(tempGoods);
				player.getShip().setCargo(player.getShip().getCargo() + 1);
				currentSystem.setGoods(tempGoods2);
				player.setCredits(player.getCredits() - buyPrices[index]);
			}
			updateMarketPlace(player);
		}
	}

	/**
	 * @author Araya Zaesim, Stanley Ayzenberg, Adam Perez, Michelle Partogi
	 */
	private class SellAll implements ActionListener {
		/**
		 * toString method.
		
		 * @return String */
		public String toString() {
			return "";
		}
		
		/**
		 * Field index.
		 */
		private final int index;

		/**
		 * Constructor for SellAll.
		 * @param index int
		 */
		private SellAll(int index) {
			this.index = index;
		}

		/**
		 * Method actionPerformed.
		 * @param e ActionEvent
		
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
		public void actionPerformed(ActionEvent e) {
			final int[] tempGoods = player.getShip().getGoods();
			final int[] tempGoods2 = currentSystem.getGoods();
			while (tempGoods[index] > 0
					&& Goods.WATER.getMTLU() <= currentSystem.getTechLevel()) {
				tempGoods[index] -= 1;
				tempGoods2[index] += 1;
				currentSystem.setGoods(tempGoods2);
				player.getShip().setGoods(tempGoods);
				player.getShip().setCargo(player.getShip().getCargo() - 1);
				player.setCredits(player.getCredits() + sellPrices[index]);
			}
			updateMarketPlace(player);
		}
	}// end sellall

	/**
	 * @author Araya Zaesim, Stanley Ayzenberg, Adam Perez, Michelle Partogi
	 */
	private class BuyOne implements ActionListener {
		/**
		 * toString method.
		
		 * @return String */
		public String toString() {
			return "";
		}
		
		/**
		 * Field index.
		 */
		private final int index;

		/**
		 * Constructor for BuyOne.
		 * @param index int
		 */
		private BuyOne(int index) {
			this.index = index;
		}

		/**
		 * Method actionPerformed.
		 * @param e ActionEvent
		
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
		public void actionPerformed(ActionEvent e) {
			final int[] tempGoods = player.getShip().getGoods();
			final int[] tempGoods2 = currentSystem.getGoods();
			final boolean isFalse = false;
			if (player.getShip().isFull() == isFalse
					&& player.getCredits() >= buyPrices[index]
					&& tempGoods2[index] > 0) { // ADD PRICE for credits
				tempGoods[index] += 1;
				tempGoods2[index] -= 1;
				player.getShip().setGoods(tempGoods);
				player.getShip().setCargo(player.getShip().getCargo() + 1);
				currentSystem.setGoods(tempGoods2);
				player.setCredits(player.getCredits() - buyPrices[index]);
				updateMarketPlace(player);
			}
		}
	}// end BuyOne

	/**
	 * Method getEvent.
	
	 * @return String */
	public String getEvent() {
		return event;
	}

	/**
	 * Method getBuyPrices.
	
	 * @return int[] */
	public int[] getBuyPrices() {
		return buyPrices;
	}

	/**
	 * Method setBuyPrices.
	 * @param buyPrices int[]
	 */
	public void setBuyPrices(int[] buyPrices) {
		this.buyPrices = buyPrices.clone();
		for (int i = 0; i < NUM_GOODS; i++) {
			lblBuyPrice[i] = new JLabel("   $" + buyPrices[i]);
		}

	}

	/**
	 * Method getSellPrices.
	
	 * @return int[] */
	public int[] getSellPrices() {
		return sellPrices;
	}

	/**
	 * Method setSellPrices.
	 * @param sellPrices int[]
	 */
	public void setSellPrices(int[] sellPrices) {
		this.sellPrices = sellPrices.clone();
		for (int i = 0; i < NUM_GOODS; i++) {
			// creates all the Goods Selling Price Labels
			lblSellPrice[i] = new JLabel("   $" + sellPrices[i]);
		}
	}

	/**
	 * Method getCurrentSystem.
	
	 * @return SolarSystem */
	public SolarSystem getCurrentSystem() {
		return currentSystem;
	}

	/**
	 * Method setCurrentSystem.
	 * @param currentSystem SolarSystem
	 */
	public void setCurrentSystem(SolarSystem currentSystem) {
		this.currentSystem = currentSystem;
	}
	
	/**
	 * Method getBtnBuyAll.
	
	 * @return JButton[]
	 */
	public JButton[] getBtnBuyAll() {
		return btnBuyAll;
	}

	/**
	 * Method getBtnSellAll.
	
	 * @return JButton[]
	 */
	public JButton[] getBtnSellAll() {
		return btnSellAll;
	}

	/**
	 * Method getBtnSellOne.
	
	 * @return JButton[]
	 */
	public JButton[] getBtnSellOne() {
		return btnSellOne;
	}

	/**
	 * Method getBtnBuyOne.
	
	 * @return JButton[]
	 */
	public JButton[] getBtnBuyOne() {
		return btnBuyOne;
	}

}
